package top.yifengxiner.framework.shiro.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.yifengxiner.system.service.SysUserService;
import top.yifengxiner.common.core.pojo.AjaxResult;
import top.yifengxiner.common.core.pojo.entity.SysUser;
import top.yifengxiner.common.core.pojo.vo.RegisterBody;
import top.yifengxiner.common.utils.ShiroUtils;

/**
 * @author ：baixiao
 * @date ：Created in 2021/5/6 10:15
 * @description：注册服务
 * @modified By：
 * @version: 0.0.1$
 */
@Component
public class SysRegisterService
{
    @Autowired
    private SysUserService userService;

    @Autowired
    private SysPasswordService passwordService;
    /**
     * 注册方法
     * @return
     */
    public AjaxResult register(RegisterBody registerBody)
    {
        if(registerBody.getUsername() == null)
        {
            return AjaxResult.warn("用户名为空");
        }
        if(registerBody.getPassword() == null)
        {
            return AjaxResult.warn("密码为空");
        }
        SysUser user = new SysUser();
        user.setUserName(registerBody.getUsername());
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(registerBody.getUsername(),registerBody.getPassword(),user.getSalt()));
        boolean res = userService.registerUser(user);
        if(res)
        {
            return AjaxResult.success("注册成功");
        }
        return AjaxResult.error("用户名或密码错误");
    }


}
